<?php
/*
 * Created on 2016-11-18
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
*/
 

class vr_node_xml
{
	private $heading = "" ;
	private $param = array() ;
	private $nodelist = array() ;
	private $values = "" ;
	
	function clear(){
		$this->heading = "" ;
		$this->param = array() ;
		$this->nodelist = array() ;
		$this->values = "" ;
	}
	function init_head($head){
		$this->heading = $head ;
	}
	function add_param($key , $val = null){
		if (is_array($key) && is_null($val)){
			foreach($key as $k=>$v){
				$this->param[$k] = $v ;
			}
		}
		elseif (is_string($key) && is_string($val)){
			$this->param[$key] = $val ;
		}
	}
	function add_node($node)
	{
		if (gettype($node) == gettype($this)){
			$this->nodelist[] = $node ;
		}
	}
	function add_line_value($str_line){
		$this->values .= "$str_line"."\r\n" ;
	}
	function getNodes()
	{
		$rnt = "" ;
		$rnt .= "<".$this->heading . " " ;
		foreach($this->param as $k=>$v){
			$rnt .= $k . "=\"" . $v . "\" " ;
		}
		$rnt .= ">\r\n" ;
		
		$rnt .= $this->values ;
		
		for($i = 0 ; $i < count($this->nodelist) ; ++ $i){
			$rnt .= $this->nodelist[$i]->getNodes() ;
		}
		
		$rnt .= "</".$this->heading . ">\r\n" ;
		
		return $rnt ;
	}
} 
 
class vr_create_newxml
{
	public $create_tz_id = 0 ;
	public $root_node = null ;
	public $desc_file = "" ;
	public $name_title_dict = array() ;

	public function create_taozhuang_vr($tz_id)
	{
		if (intval(sql_fetch_one_cell("select count(*) from taozhuang_vr_buffer where tzid = $tz_id")) <= 0){
			return "" ;
		}
		
		$this->desc_file = $GLOBALS['VR_XML_PATH'] . "gjj_" . $tz_id . ".xml" ;
		
		$this->create_tz_id = $tz_id ;
		$this->root_node = new vr_node_xml() ;
		$this->root_node->init_head("krpano") ;

		$this->set_first_load_xml() ;
		$this->set_style() ;
		$this->create_set_scene() ;
		
		if (file_exists($this->desc_file)){
			unlink($this->desc_file) ;
		}
		file_put_contents($this->desc_file,$this->root_node->getNodes(),FILE_APPEND);
		
		return $GLOBALS['VR_XML_URL'] . "gjj_" . $tz_id . ".xml" ;
	}
	public function get_taozhuang_vr_url($tz_id)
	{
		$this->desc_file = $GLOBALS['VR_XML_PATH'] . "gjj_" . $tz_id . ".xml" ;
		
		if (!file_exists($this->desc_file))
		{
			return $this->create_taozhuang_vr($tz_id) ;
		}
		
		return $GLOBALS['VR_XML_URL'] . "gjj_" . $tz_id . ".xml" ;
	}

	// 装载初始要装载的 xml 字符串
	private function set_first_load_xml()
	{
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "loadinganimation.xml") ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "loadingpercenttext.xml") ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "layer_action.xml") ;
		$this->root_node->add_node($nd) ;
				
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "action.xml") ;
		$this->root_node->add_node($nd) ;
				
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "skin/vtourskin.xml") ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;		$nd->init_head("include") ;		$nd->add_param("url" , "webvr_action.xml") ;
		$nd->add_param("if" , "mode == webvr") ;
		$this->root_node->add_node($nd) ;
	}

	// 装载启动node
	private function add_action_menu($action_name , $count)
	{
		/**
		 * startup------------------------------------------
		 */
		$nd = new vr_node_xml() ;		$nd->init_head("action") ;		$nd->add_param("name" , $action_name) ;

		$startin = "start_".$action_name ;
		$endin = "end_".$action_name ;

		$nd->add_line_value("if(menuMode === true,set(start_alpha, 1.0);	set(start_bg_alpha, 0.5);	set(end_alpha, 0.0);	set(start_tt, 3.0);") ;
		$nd->add_line_value("		menuIn($startin,get(end_alpha),get(start_alpha),get(start_tt),true);") ;

		for($i = 1 ; $i <= $count ; ++ $i){
			$nm = $action_name."$i" ;
			$nd->add_line_value("		menuOut($nm,get(start_bg_alpha),get(end_alpha),get(start_tt),false);") ;			
			$nm = $action_name."$i"."thumb" ;
			$nd->add_line_value("		menuOut($nm,get(start_alpha),get(end_alpha),get(start_tt),true);") ;			
			$nm = $action_name."$i"."txt" ;
			$nd->add_line_value("		menuOut($nm,get(start_alpha),get(end_alpha),get(start_tt),false);") ;
		}

		$nd->add_line_value("		menuOut($endin,get(start_alpha),get(end_alpha),get(start_tt),true);") ;
		$nd->add_line_value("		set(menuMode, false);") ;
		$nd->add_line_value("	,") ;


		$nd->add_line_value("	set(start_alpha, 0.0);	set(end_bg_alpha, 0.5);		set(end_alpha, 1.0);		set(start_tt, 3.0);") ;
		$nd->add_line_value("	menuOut($startin,get(end_alpha),get(start_alpha),get(start_tt),true);") ;

		for($i = 1 ; $i <= $count ; ++ $i){
			$nm = $action_name."$i" ;
			$nd->add_line_value("	menuIn($nm,get(start_alpha),get(end_bg_alpha),get(start_tt),false);") ;
			$nm = $action_name."$i"."thumb" ;
			$nd->add_line_value("	menuIn($nm,get(start_alpha),get(end_alpha),get(start_tt),true);") ;
			$nm = $action_name."$i"."txt" ;
			$nd->add_line_value("	menuIn($nm,get(start_alpha),get(end_alpha),get(start_tt),false);") ;
		}		
		$nd->add_line_value("	menuIn($endin,get(start_alpha),get(end_alpha),get(start_tt),true);") ;
		$nd->add_line_value("	set(menuMode, true);") ;
		$nd->add_line_value(");") ;		
		$this->root_node->add_node($nd) ;
	}	
		
	private function set_style()
	{
		$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "menu_enter") ;
		$nd->add_param("url" , "skin/point.png") ;
		$nd->add_param("onloaded" , "do_crop_animation(82,82, 5);" ) ;
		$nd->add_param(array("canRemove"=>"true","needClose"=>"true","distorted"=>"true","enabled"=>"true","zorder"=>"1","capture"=>"false","alpha"=>"1.0","depth"=>"700","ty"=>"35")) ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "menu_exit") ;
		$nd->add_param("url" , "hot_close.png") ;
		$nd->add_param(array("canRemove"=>"true","distorted"=>"true","enabled"=>"false","zorder"=>"1","capture"=>"false","alpha"=>"0.0","depth"=>"700","ty"=>"35")) ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "menu_change") ;
		$nd->add_param("url" , "hotspot_exit.png") ;
		$nd->add_param(array("canRemove"=>"true","needClose"=>"true","distorted"=>"true","enabled"=>"true","zorder"=>"1","capture"=>"false","alpha"=>"1.0","depth"=>"700")) ;
		$this->root_node->add_node($nd) ;

		$gotoname = array("qkt" , "qzw" , "qcw" , "qsf" , "qcf" , "qct" , "qwsj") ;
		$gotoarrs = array("去客厅.png" , "去主卧.png" , "去次卧.png" , "去书房.png" , "去厨房.png" , "去餐厅.png" , "去卫生间.png") ;
		for($i = 0 ; $i < count($gotoname) ; ++ $i){
			$mm = $gotoname[$i] ;
			$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "m_c_".$mm) ;
			$nd->add_param("url" , "skin/" . $mm . ".png") ;
			$nd->add_param(array("canRemove"=>"true","needClose"=>"true","distorted"=>"true","enabled"=>"true","zorder"=>"1","capture"=>"false","alpha"=>"1.0","depth"=>"700")) ;
			$this->root_node->add_node($nd) ;
		}
		
		$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "panel") ;
		$nd->add_param("url" , "black.png") ;
		$nd->add_param(array("canRemove"=>"true","distorted"=>"true","enabled"=>"false","zorder"=>"1","width"=>"100","height"=>"100","alpha"=>"0.0","depth"=>"700","ty"=>"35")) ;
		$this->root_node->add_node($nd) ;
		
		$nd = new vr_node_xml() ;	$nd->init_head("style") ;	$nd->add_param("name" , "thumb") ;		
		$nd->add_param(array("canRemove"=>"true","distorted"=>"true","enabled"=>"false","capture"=>"false","alpha"=>"0.0","depth"=>"700","ty"=>"35","onover"=>"tween(depth,680);","onout"=>"tween(depth,700);")) ;
		$this->root_node->add_node($nd) ;		
		
		
	}
			
	// 定义及创建默认显示VR Scene 面板
	private function create_set_scene()
	{
		$tzid = $this->create_tz_id ;
		$rows = sql_fetch_rows("select * from taozhuang_vr_buffer where tzid = $tzid") ;
		
		if (is_array($rows) && count($rows) > 0){
			foreach($rows as $r){
				$this->create_one_group($r) ;
			}
			return count($rows) ;
		}
		return 0 ;
	}
	private function create_one_group($buff_item)
	{
		$scenename = "scene_".$buff_item['id'] ;
		
		$addrimg = array() ;
		$rt_img = util_set_IMAGE_one($addrimg , $buff_item['img_b']) ;
		if (is_array($rt_img) && array_key_exists('img_obj' , $rt_img) && array_key_exists('url' , $rt_img['img_obj'])){
			$imgpath = $rt_img['img_obj']['url'] ;
		}else{
			return ;
		}
		
		$nd = new vr_node_xml() ;	$nd->init_head("scene") ;	
		$nd->add_param("name" , $scenename) ;
		$nd->add_param("title" , $scenename) ;
		$nd->add_param("onstart" , "") ;
				
		$nd_v = new vr_node_xml() ;		$nd_v->init_head("view") ;
		$nd_v->add_param(array("hlookat"=>"0","vlookat"=>"0","fovtype"=>"MFOV","fov"=>"95","fovmin"=>"45","fovmax"=>"120")) ;

		$img = new vr_node_xml() ;		$img->init_head("image") ;
		$cube = new vr_node_xml() ;		$cube->init_head("cube") ;
		$cube->add_param("url" , str_replace("_b.jpg" , "_%s.jpg" , $imgpath)) ;
		$img->add_node($cube) ;

		$nd->add_node($nd_v) ;
		$nd->add_node($img) ;		

		$this->create_set_panel($buff_item , $nd) ;

		$this->root_node->add_node($nd) ;
		
	}
	// 定义及创建选择面板
	private function create_set_panel($buff_item , &$scene_node)
	{
		$buff_str_list = $buff_item['pos_list'] ;
		$poslist = sql_fetch_rows("select * from taozhuang_vr_pos where `id` in ( $buff_str_list )") ;
					
		foreach($poslist as $p){
			$posarr = explode("," , $buff_str_list) ;
			$id = $p['id'] ;
			$xb = 0 ;
			for($i = 0 ; $i < count($posarr) ; ++ $i){
				if (intval($posarr[$i]) == $id){
					$xb = $i ;
					break ;
				}
			}
			
			$pos = $p['pos'] ;
			$tzid = $p['tzid'] ;
			
			$posxy = sql_fetch_rows("select * from taozhuang_vr_exch where `tzid` = '$tzid' and `pos` <= '0'") ;
			if (is_array($posxy) && count($posxy) > 0){
				$inum = 1 ;
				foreach($posxy as $p){
					$posx = $p['x'] ;
					$posy = $p['y'] ;
					$gotourl = $p['goto_url'] ;
					$gotoimg = $p['goto_img'] ;
					if (strlen($gotoimg) <= 5)	$gotoimg = "menu_change" ;
					
					$gotoname = array("qkt" , "qzw" , "qcw" , "qsf" , "qcf" , "qct" , "qwsj") ;
					$gotoarrs = array("去客厅.png" , "去主卧.png" , "去次卧.png" , "去书房.png" , "去厨房.png" , "去餐厅.png" , "去卫生间.png") ;
					for($i = 0 ; $i <= count($gotoarrs) ; ++ $i)
					{
						if ($gotoimg == $gotoarrs[$i]){
							$gotoimg = "m_c_".$gotoname[$i] ;
							break ;
						}
					}
					
					$gotourl = $GLOBALS['VR_XML_URL'] . "gjj_" . $gotourl . ".xml" ;
					
					$menuname = "menu_".$buff_item['id']."goto".$inum ;
					
					$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "tt_$menuname") ;
					$nd->add_param("onclick" , "changeScene('$gotourl');") ;
					$nd->add_param(array("style"=>"$gotoimg","ath"=>"$posx","atv"=>"$posy")) ;				
					$scene_node->add_node($nd) ;
				
					$inum ++ ;	
				}
			}
			
			$ord = sql_fetch_rows("select * from taozhuang_vr_pos where `tzid` = '$tzid' and `pos` = '$pos'") ;
			if (is_array($ord) && count($ord) > 1)
			{
				$menuname = "menu_".$buff_item['id']."_pos_".$pos."_" ;
				$this->add_action_menu($menuname , count($ord)) ;
		
				$posxy = sql_fetch_one("select * from taozhuang_vr_exch where `tzid` = '$tzid' and `pos` = '$pos'") ;
				if (is_array($posxy) == false || is_null($posxy) == true){
					continue ;
				}
				$posx = intval($posxy['x']) ;
				$posy = intval($posxy['y']) ;
				
				$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "start_$menuname") ;
				$nd->add_param(array("style"=>"menu_enter","ath"=>"$posx","atv"=>"$posy","onclick"=>"$menuname();")) ;				
				$scene_node->add_node($nd) ;
				
				$item_buff_dict = array() ;
				foreach($ord as $o){
					$posarr[$xb] = $o['id'] ;
					$sel_pos_list = implode("," , $posarr) ;
					$buffid = sql_fetch_one_cell("select id from taozhuang_vr_buffer where `tzid` = '$tzid' and `pos_list` = '$sel_pos_list'") ;					
					$item_buff_dict[$o['item_id']] = $buffid ;
				}

				$i = 0 ;
				$ath = $posx ;
				$atv = $posy ;
				$www = 20 ;
				foreach($ord as $o){
					$i ++ ;
					$item_id = $o['item_id'] ;
					$item = sql_fetch_one("select * from sys_item where `id` = $item_id") ;
			
					$addrimg = array() ;
					$rt_img = util_set_IMAGE_one($addrimg , $item['img_url_id']) ;
					$imgpath = $rt_img['img_obj']['url'] ;
					$gotoseconname = "scene_".$item_buff_dict[$item_id] ;
					
					$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "$menuname".$i) ;
					$nd->add_param(array("style"=>"panel","ath"=>"$ath","atv"=>"$atv")) ;				
					$scene_node->add_node($nd) ;
					
					$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "$menuname".$i."thumb") ;
					$nd->add_param(array("style"=>"thumb","zorder"=>"3","ath"=>"$ath","atv"=>"$atv","url"=>$imgpath,"scale"=>"1","width"=>"80","height"=>"80","ox"=>"0","oy"=>"0","onclick"=>"changeImage($gotoseconname);")) ;				
					$scene_node->add_node($nd) ;
					
//					$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "$menuname".$i."txt") ;
//					$nd->add_param(array("style"=>"thumb","zorder"=>"2","ath"=>"$ath","atv"=>"$atv","url"=>"texts.png","crop"=>"4|360|840|88","scale"=>"0.15","oy"=>"40","enabled"=>"false")) ;				
//					$scene_node->add_node($nd) ;
					
					if ($ath == $posx)					$ath = $posx + $www * -1 ;
					elseif ($ath == $posx + $www * -1)	$ath = $posx + $www * 1;
					elseif ($ath == $posx + $www * 1)	$ath = $posx + $www * -2 ;
					elseif ($ath == $posx + $www * -2)	$ath = $posx + $www * 2 ;
					elseif ($ath == $posx + $www * 2)	$ath = $posx + $www * -3 ;
					elseif ($ath == $posx + $www * -3)	$ath = $posx + $www * 3 ;
					elseif ($ath == $posx + $www * 3)	$ath = $posx + $www * -4 ;
					elseif ($ath == $posx + $www * -4)	$ath = $posx + $www * 4 ;
					elseif ($ath == $posx + $www * 4)	$ath = $posx + $www * -5 ;
					elseif ($ath == $posx + $www * -5)	$ath = $posx + $www * 5 ;
					elseif ($ath == $posx + $www * 5)	$ath = $posx + $www * -6 ;
					elseif ($ath == $posx + $www * -6)	$ath = $posx + $www * 6 ;
					elseif ($ath == $posx + $www * 6)	$ath = $posx + $www * -7 ;
					elseif ($ath == $posx + $www * -7)	$ath = $posx + $www * 7 ;
				}
				
				$ot_x = $posx ;
				$ot_y = $posy + 15 ;
				
				$nd = new vr_node_xml() ;	$nd->init_head("hotspot") ;	$nd->add_param("name" , "end_$menuname") ;
				$nd->add_param(array("style"=>"menu_exit","ath"=>"$ot_x","atv"=>"$ot_y","scale"=>"0.5","onclick"=>"$menuname();")) ;				
				$scene_node->add_node($nd) ;
			}
		}
	}
}

function vr_create_vrXML($json)
{
	$tzid = intval(get_jsonValue($json,"tzid")) ; 	
	$ret = array() ;
	$ret['data'] = array() ;	
	
	try{
		$vr = new vr_create_newxml() ;
		$url = $vr->create_taozhuang_vr($tzid) ;
		$ret['data']['xml_file'] = $vr->desc_file ;
		$ret['data']['xml_url'] = $url ;
	}catch(Exception $e){		
		$err = $e->getMessage() . "\r\n" . exceptionToString($e) ;
		
		$ret['data']['xml_file'] = $err ;
	}
	return $ret ;
}

function vr_get_and_create_vrXML($json)
{
	$tzid = intval(get_jsonValue($json,"tzid")) ; 	
	$ret = array() ;
	$ret['data'] = array() ;	
	
	try{
		$vr = new vr_create_newxml() ;
		$url = $vr->get_taozhuang_vr_url($tzid) ;
		
		$ret['data']['xml_file'] = $vr->desc_file ;
		$ret['data']['xml_url'] = $url ;
	}catch(Exception $e){		
		$err = $e->getMessage() . "\r\n" . exceptionToString($e) ;
		
		$ret['data']['xml_file'] = $err ;
	}
	return $ret ;
	
}

?>
